﻿using System;
using System.Collections.Generic;
using System.Text;

using System.Data.Odbc;
using System.Data;
using System.Xml.Serialization;
using WLMCoreLibrary;
using System.Configuration;

namespace MiWebSite_Core
{
	public class GeoTag : BaseClass
	{
		

		#region public properties
		public String name {get;set; }
		public double Latitud {get;set; }
		public double Longitud {get;set; }
		#endregion

		#region constructors
		public GeoTag()
		{
		}

		public GeoTag(ref dbConnector ConexionDB)
		{
			ConexionDBInterna = ConexionDB;
			WLMClassBinder classBinder = new WLMClassBinder(ref ConexionDBInterna);
		id = classBinder.NewRecord("GEOTAG");
			string queryString = "";
			queryString = "SELECT * FROM GEOTAG WHERE id='" + id + "'";
			GetRecord(queryString);
		}

		public GeoTag(int registryId, ref dbConnector ConexionDB)
		{
			ConexionDBInterna = ConexionDB;
			string queryString = "";
			queryString = "SELECT * FROM GEOTAG WHERE id='" + registryId + "'";
			WLMClassBinder classBinder = new WLMClassBinder(ref ConexionDBInterna);
			this.GetRecord(queryString);
		}
		public GeoTag(DataRow dataRow, dbConnector dbConn)
		{
			this.GetRecord(dataRow);
		}
		#endregion

		#region public methods
		public void Delete()
		{
			string queryString = "";
			queryString = "DELETE FROM GEOTAG WHERE id='" + id + "'";
			ConexionDBInterna.ExecuteCommand(queryString);
		}
		#endregion

		#region Private Methods
		private void GetRecord(string queryString)
		{
			OdbcDataReader reader = ConexionDBInterna.ExecuteReader(queryString);
			if (reader.HasRows)
			{
				if (!reader.IsDBNull(reader.GetOrdinal("id")))
				id = (System.Int32)reader["id"];
				if (!reader.IsDBNull(reader.GetOrdinal("fechaCreacion")))
				fechaCreacion = Convert.ToDateTime(reader["fechaCreacion"]);
				if (!reader.IsDBNull(reader.GetOrdinal("fechaModificacion")))
				fechaModificacion = Convert.ToDateTime(reader["fechaModificacion"]);
				if (!reader.IsDBNull(reader.GetOrdinal("idUsuario")))
				idUsuario = (System.Int32)reader["idUsuario"];
				if (!reader.IsDBNull(reader.GetOrdinal("name")))
				name = (System.String)reader["name"];
				if (!reader.IsDBNull(reader.GetOrdinal("lat")))
				Latitud = (System.Double)reader["lat"];
				if (!reader.IsDBNull(reader.GetOrdinal("lon")))
				Longitud = (System.Double)reader["lon"];
			}
		}


		private void GetRecord(DataRow dataRow)
		{
			if (dataRow != null)
			{
				if (dataRow["id"] != null)
				id = (System.Int32)dataRow["id"];
				if (dataRow["fechaCreacion"] != null)
				fechaCreacion = (System.DateTime)dataRow["fechaCreacion"];
				if (dataRow["fechaModificacion"] != null)
				fechaModificacion = (System.DateTime)dataRow["fechaModificacion"];
				if (dataRow["idUsuario"] != null)
				idUsuario = (System.Int32)dataRow["idUsuario"];
				if (dataRow["name"] != null)
				name = (System.String)dataRow["name"];
				if (dataRow["lat"] != null)
				Latitud = (System.Double)dataRow["lat"];
				if (dataRow["lon"] != null)
				Longitud = (System.Double)dataRow["lon"];
			}
		}


		protected override void UpdateBind()
		{
			StringBuilder strCommand=new StringBuilder();
			strCommand.Append("UPDATE GEOTAG set ");
			strCommand.Append("fechaModificacion='" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "'");
			strCommand.Append(",name=\"" + WLMFunctions.comillas(name) + "\"");
			strCommand.Append(",lat=\"" + WLMFunctions.comillas(Latitud.ToString()) + "\"");
			strCommand.Append(",lon=\"" + WLMFunctions.comillas(Longitud.ToString()) + "\"");
			strCommand.Append(" WHERE id='" + id + "'");
			ConexionDBInterna.ExecuteCommand(strCommand.ToString());
		}
		#endregion
	}




	public class GeoTagCollection
	{
		private dbConnector ConexionDB;
		private BaseClassArray<GeoTag> _items;
        public BaseClassArray<GeoTag> Elementos { get; set; }
		public GeoTagCollection()
		{
		}
		public GeoTagCollection(ref dbConnector dbConn)
		{

			string strCommand = "SELECT id FROM GEOTAG ";
			OdbcDataReader dataReader = ConexionDB.ExecuteReader(strCommand);
		Elementos = new BaseClassArray<GeoTag>();
			while (dataReader.Read())
			{
				GeoTag newItem = new GeoTag(Convert.ToInt32(dataReader["id"]), ref ConexionDB);
			Elementos.Add(newItem);
			}
			dataReader.Close();
			dataReader.Dispose();
		}
	}
}
